﻿using STMS.DAL;
using STMS.Models.DModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STMS.BLL
{
    public class StoreBLL
    {
        private StoreDAL storeDAL = new StoreDAL();

        /// <summary>
        /// 指定仓库是否添加了分区
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public bool IsAddStoreRegions(int storeId)
        {
            bool bl = false;
            DataTable dt = storeDAL.IsAddRegion(storeId);
            int count1 = (int)dt.Rows[0][0];
            if (count1 == 0)
                bl = false;
            else if (count1 > 0)
                bl = true;
            return bl;
        }

        /// <summary>
        /// 关键词查询仓库列表
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public List<StoreInfo> GetStoreList(string keywords, bool isShowDel)
        {
            int isDeleted = isShowDel ? 1 : 0;
            return storeDAL.GetStoreList(keywords, isDeleted);
        }

        /// <summary>
        /// 获取所有仓库列表  绑定下拉框
        /// </summary>
        /// <returns></returns>
        public List<StoreInfo> GetAllStores()
        {
            return storeDAL.GetAllStores();
        }
        /// <summary>
        /// 获取指定的仓库信息
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public StoreInfo GetStore(int storeId)
        {
            return storeDAL.GetStore(storeId);
        }
        /// <summary>
        /// 判断仓库是否已存在
        /// </summary>
        /// <param name="storeName"></param>
        /// <returns></returns>
        public bool ExistsStore(string storeName)
        {
            return storeDAL.ExistsStore(storeName);
        }

        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="store"></param>
        /// <param name="isGetId"></param>
        /// <returns></returns>
        public bool AddStore(StoreInfo store)
        {
            return storeDAL.AddStore(store, 0) > 0;
        }

        public int AddStoreWithId(StoreInfo store)
        {
            return storeDAL.AddStore(store, 1);
        }

        /// <summary>
        /// 修改仓库信息
        /// </summary>
        /// <param name="store"></param>
        /// <returns></returns>
        public bool UpdateStore(StoreInfo store)
        {
            return storeDAL.UpdateStore(store);
        }

        /// <summary>
        /// 仓库删除  真删除 
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns>0 删除失败，已添加了分区  1  删除成功</returns>
        public bool DeleteStore(int storeId)
        {
            return storeDAL.Delete(storeId, 1, 2);
        }

        /// <summary>
        /// 删除仓库  批量  真删除
        /// </summary>
        /// <param name="storeIds"></param>
        /// <returns></returns>
        public int DeleteStore(List<int> storeIds)
        {
            int re = 0;
            re = storeDAL.DeleteList(storeIds, 1, 2) ? 1 : 0;
            return re;
        }

        /// <summary>
        /// 逻辑删除仓库信息  假删除
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public int LogicDeleteStore(int storeId)
        {
            int re = 0;
            if (IsAddStoreRegions(storeId))
                re = 2;//表示已添加了分区
            else
            {
                bool bl = storeDAL.Delete(storeId, 0, 1);
                if (bl)
                    re = 1;//删除成功   
            }
            return re;
        }

        public string LogicDeleteStore(List<int> storeIds)
        {
            string reStr = "";
            foreach (int id in storeIds)
            {
                if (IsAddStoreRegions(id))
                {
                    if (reStr != "") reStr += ",";
                    //表示已添加了分区
                    reStr +=id.ToString();
                }
            }
            if (reStr =="")
                reStr = storeDAL.DeleteList(storeIds, 0, 1) ? "" : "0";
            return reStr;
        }

        /// <summary>
        /// 恢复仓库信息
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public bool RecoverStore(int storeId)
        {
            return storeDAL.Delete(storeId, 0, 0);
        }
    }
}
